home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdAdvancedEdit.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  10.2 KB  |  353 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Ben "Count XULula" Goodger
  22.  */
  23.  
  24. /**************         GLOBALS         **************/
  25. var gElement    = null; // handle to actual element edited
  26.  
  27. var HTMLAttrs   = [];   // html attributes
  28. var CSSAttrs    = [];   // css attributes
  29. var JSEAttrs    = [];   // js events
  30.  
  31. var HTMLRAttrs  = [];   // removed html attributes
  32. var JSERAttrs   = [];   // removed js events
  33.  
  34. /* Set false to allow changing selection in tree
  35.    without doing "onselect" handler actions
  36. */
  37. var gDoOnSelectTree = true;
  38. var gUpdateTreeValue = true;
  39.  
  40. /************** INITIALISATION && SETUP **************/
  41.  
  42. /**
  43.  * function   : void Startup();
  44.  * parameters : none
  45.  * returns    : none
  46.  * desc.      : startup and initialisation, prepares dialog.
  47.  **/
  48. function Startup()
  49. {
  50.   // This is the return value for the parent,
  51.   // who only needs to know if OK was clicked
  52.   window.opener.AdvancedEditOK = false;
  53.  
  54.   if (!InitEditorShell())
  55.     return;
  56.  
  57.   // Element to edit is passed in
  58.   if (!window.arguments[1])
  59.   {
  60.     dump("Advanced Edit: Element to edit not supplied\n");
  61.     window.close();
  62.     return;
  63.   }
  64.   // The actual element edited (not a copy!)
  65.   gElement = window.arguments[1];
  66.  
  67.   // place the tag name in the header
  68.   var tagLabel = document.getElementById("tagLabel");
  69.   tagLabel.setAttribute("value", ("<" + gElement.localName + ">"));
  70.  
  71.   // Create dialog object to store controls for easy access
  72.   gDialog.AddHTMLAttributeNameInput  = document.getElementById("AddHTMLAttributeNameInput");
  73.  
  74.   // We use a <deck> to switch between editable menulist and textbox
  75.   gDialog.AddHTMLAttributeValueDeck     = document.getElementById("AddHTMLAttributeValueDeck");
  76.   gDialog.AddHTMLAttributeValueMenulist = document.getElementById("AddHTMLAttributeValueMenulist");
  77.   gDialog.AddHTMLAttributeValueTextbox  = document.getElementById("AddHTMLAttributeValueTextbox");
  78.   gDialog.AddHTMLAttributeValueInput    = gDialog.AddHTMLAttributeValueTextbox;
  79.  
  80.   gDialog.AddHTMLAttributeTree          = document.getElementById("HTMLATree");
  81.   gDialog.AddCSSAttributeNameInput      = document.getElementById("AddCSSAttributeNameInput");
  82.   gDialog.AddCSSAttributeValueInput     = document.getElementById("AddCSSAttributeValueInput");
  83.   gDialog.AddCSSAttributeTree           = document.getElementById("CSSATree");
  84.   gDialog.AddJSEAttributeNameList       = document.getElementById("AddJSEAttributeNameList");
  85.   gDialog.AddJSEAttributeValueInput     = document.getElementById("AddJSEAttributeValueInput");
  86.   gDialog.AddJSEAttributeTree           = document.getElementById("JSEATree");
  87.   gDialog.okButton                      = document.documentElement.getButton("accept");
  88.  
  89.   // build the attribute trees
  90.   BuildHTMLAttributeTable();
  91.   BuildCSSAttributeTable();
  92.   BuildJSEAttributeTable();
  93.   
  94.   // Build attribute name arrays for menulists
  95.   BuildJSEAttributeNameList();
  96.   BuildHTMLAttributeNameList();
  97.   // No menulists for CSS panel (yet)
  98.  
  99.   // Set focus to Name editable menulist in HTML panel
  100.   SetTextboxFocus(gDialog.AddHTMLAttributeNameInput);
  101.  
  102.   // size the dialog properly
  103.   window.sizeToContent();
  104.  
  105.   SetWindowLocation();
  106. }
  107.  
  108. /**
  109.  * function   : bool onOK ( void );
  110.  * parameters : none
  111.  * returns    : boolean true to close the window
  112.  * desc.      : event handler for ok button
  113.  **/
  114. function onAccept()
  115. {
  116.   editorShell.BeginBatchChanges();
  117.   try {
  118.     // Update our gElement attributes
  119.     UpdateHTMLAttributes();
  120.     UpdateCSSAttributes();
  121.     UpdateJSEAttributes();
  122.   } catch(ex) {
  123.     dump(ex);
  124.   }
  125.   editorShell.EndBatchChanges();
  126.  
  127.   window.opener.AdvancedEditOK = true;
  128.   SaveWindowLocation();
  129.  
  130.   return true; // do close the window
  131. }
  132.  
  133. // Helpers for removing and setting attributes
  134. // Use editor transactions if modifying the element already in the document
  135. // (Temporary element from a property dialog won't have a parent node)
  136. function doRemoveAttribute(attrib)
  137. {
  138.   if (gElement.parentNode)
  139.     editorShell.RemoveAttribute(gElement, attrib);
  140.   else
  141.     gElement.removeAttribute(attrib);
  142. }
  143.  
  144. function doSetAttribute(attrib, value)
  145. {
  146.   if (gElement.parentNode)
  147.     editorShell.SetAttribute(gElement, attrib, value);
  148.   else
  149.     gElement.setAttribute(attrib, value);
  150. }
  151.  
  152. /**
  153.  * function   : bool CheckAttributeNameSimilarity ( string attName, array attArray );
  154.  * parameters : attribute to look for, array of current attributes
  155.  * returns    : true if attribute already exists, false if it does not
  156.  * desc.      : checks to see if any other attributes by the same name as the arg supplied
  157.  *              already exist.
  158.  **/
  159. function CheckAttributeNameSimilarity(attName, attArray)
  160. {
  161.   for (var i = 0; i < attArray.length; i++)
  162.   {
  163.     if (attName.toLowerCase() == attArray[i].toLowerCase())
  164.       return true;
  165.   }
  166.   return false;
  167. }
  168.  
  169. /**
  170.  * function   : bool UpdateExistingAttribute ( string attName, string attValue, string treeChildrenId );
  171.  * parameters : attribute to look for, new value, ID of <treeChildren> node in XUL tree
  172.  * returns    : true if attribute already exists in tree, false if it does not
  173.  * desc.      : checks to see if any other attributes by the same name as the arg supplied
  174.  *              already exist while setting the associated value if different from current value
  175.  **/
  176. function UpdateExistingAttribute( attName, attValue, treeChildrenId )
  177. {
  178.   var treeChildren = document.getElementById(treeChildrenId);
  179.   if (!treeChildren)
  180.     return false;
  181.  
  182.   var name;
  183.   var i;
  184.   attName = TrimString(attName).toLowerCase();
  185.   attValue = TrimString(attValue);
  186.  
  187.   for (i = 0; i < treeChildren.childNodes.length; i++)
  188.   {
  189.     var item = treeChildren.childNodes[i];
  190.     name = GetTreeItemAttributeStr(item);
  191.     if (name.toLowerCase() == attName)
  192.     {
  193.       // Set the text in the "value' column treecell
  194.       SetTreeItemValueStr(item, attValue);
  195.  
  196.       // Select item just changed, 
  197.       //  but don't trigger the tree's onSelect handler
  198.       gDoOnSelectTree = false;
  199.       try {
  200.         selectTreeItem(treeChildren, item);
  201.       } catch (e) {}
  202.       gDoOnSelectTree = true;
  203.  
  204.       return true;
  205.     }
  206.   }
  207.   return false;
  208. }
  209.  
  210. /**
  211.  * function   : string GetAndSelectExistingAttributeValue ( string attName, string treeChildrenId );
  212.  * parameters : attribute to look for, ID of <treeChildren> node in XUL tree
  213.  * returns    : value in from the tree or empty string if name not found
  214.  **/
  215. function GetAndSelectExistingAttributeValue( attName, treeChildrenId )
  216. {
  217.   if (!attName)
  218.     return "";
  219.  
  220.   var treeChildren = document.getElementById(treeChildrenId);
  221.   var name;
  222.   var i;
  223.  
  224.   for (i = 0; i < treeChildren.childNodes.length; i++)
  225.   {
  226.     var item = treeChildren.childNodes[i];
  227.     name = GetTreeItemAttributeStr(item);
  228.     if (name.toLowerCase() == attName.toLowerCase())
  229.     {
  230.       // Select item in the tree
  231.       //  but don't trigger the tree's onSelect handler
  232.       gDoOnSelectTree = false;
  233.       try {
  234.         selectTreeItem(treeChildren, item);
  235.       } catch (e) {}
  236.       gDoOnSelectTree = true;
  237.  
  238.       // Get the text in the "value' column treecell
  239.       return GetTreeItemValueStr(item);
  240.     }
  241.   }
  242.  
  243.   // Attribute doesn't exist in tree, so remove selection
  244.   gDoOnSelectTree = false;
  245.   try {
  246.     treeChildren.parentNode.treeBoxObject.selection.clearSelection();
  247.   } catch (e) {}
  248.   gDoOnSelectTree = true;
  249.  
  250.   return "";
  251. }
  252.  
  253. /* Tree structure: 
  254.   <treeItem>
  255.     <treeRow>
  256.       <treeCell> // Name Cell
  257.       <treeCell  // Value Cell
  258. */
  259. function GetTreeItemAttributeStr(treeItem)
  260. {
  261.   if (treeItem)
  262.     return TrimString(treeItem.firstChild.firstChild.getAttribute("label"));
  263.  
  264.   return "";
  265. }
  266.  
  267. function GetTreeItemValueStr(treeItem)
  268. {
  269.   if (treeItem)
  270.     return TrimString(treeItem.firstChild.lastChild.getAttribute("label"));
  271.  
  272.   return "";
  273. }
  274.  
  275. function SetTreeItemValueStr(treeItem, value)
  276. {
  277.   if (treeItem && GetTreeItemValueStr(treeItem) != value)
  278.     treeItem.firstChild.lastChild.setAttribute("label", value);
  279. }
  280.  
  281. function IsNotTreeHeader(treeCell)
  282. {
  283.   if (treeCell)
  284.     return (treeCell.parentNode.parentNode.nodeName != "treehead");
  285.  
  286.   return false;
  287. }
  288.  
  289. function RemoveNameFromAttArray(attName, attArray)
  290. {
  291.   for (var i=0; i < attArray.length; i++)
  292.   {
  293.     if (attName.toLowerCase() == attArray[i].toLowerCase())
  294.     {
  295.       // Remove 1 array item
  296.       attArray.splice(i,1);
  297.       break;
  298.     }
  299.   }
  300. }
  301.  
  302. // adds a generalised treeitem.
  303. function AddTreeItem ( name, value, treeChildrenId, attArray )
  304. {
  305.   attArray[attArray.length] = name;
  306.   var treeChildren    = document.getElementById ( treeChildrenId );
  307.   var treeitem    = document.createElementNS ( XUL_NS, "treeitem" );
  308.   var treerow     = document.createElementNS ( XUL_NS, "treerow" );
  309.  
  310.   var attrCell    = document.createElementNS ( XUL_NS, "treecell" );
  311.   attrCell.setAttribute( "class", "propertylist" );
  312.   attrCell.setAttribute( "label", name );
  313.  
  314.   var valueCell    = document.createElementNS ( XUL_NS, "treecell" );
  315.   valueCell.setAttribute( "class", "propertylist" );
  316.   valueCell.setAttribute( "label", value );
  317.  
  318.   treerow.appendChild ( attrCell );
  319.   treerow.appendChild ( valueCell );
  320.   treeitem.appendChild ( treerow );
  321.   treeChildren.appendChild ( treeitem );
  322.  
  323.   // Select item just added,
  324.   //  but suppress calling the onSelect handler
  325.   gDoOnSelectTree = false;
  326.   try {
  327.     selectTreeItem(treeChildren, item);
  328.   } catch (e) {}
  329.   gDoOnSelectTree = true;
  330.  
  331.   return treeitem;
  332. }
  333.  
  334. function doHelpButton()
  335. {
  336.   openHelp("advanced_property_editor");
  337.   return true;
  338. }
  339.  
  340. function selectTreeItem(treeChildren, item)
  341. {
  342.   var index = treeChildren.parentNode.contentView.getIndexOfItem(item);
  343.   treeChildren.parentNode.treeBoxObject.selection.select(index);
  344. }
  345.  
  346. function getSelectedItem(tree)
  347. {
  348.   if (tree.treeBoxObject.selection.count == 1)
  349.     return tree.contentView.getItemAtIndex(tree.currentIndex);
  350.   else
  351.     return null;
  352. }
  353.